package ca.uwindsor.cs.deepweb.utility.documentanalysis;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;

import ca.uwindsor.cs.deepweb.estimation.FilesContentIndexer;
import ca.uwindsor.cs.deepweb.utility.FileSizeDistributionAnalyzer;

public class LuceneIndexSizeAnalyzer {

	/**
	 * @param args
	 * @throws IOException
	 * @throws CorruptIndexException
	 */
	public static void main(String[] args) throws CorruptIndexException,
			IOException {
		if (args.length != 3) {
			System.exit(-1);
		}
		IndexReader reader = IndexReader.open(args[0]);
		// System.out.println("NumDocs = " + reader.numDocs());
		Document doc;
		int total = reader.numDocs();
		int intervel = Integer.parseInt(args[2]);
		long size;
		FileSizeDistributionAnalyzer sizeanalyzer = new FileSizeDistributionAnalyzer(
				intervel, null);
		int i;
		for (i = 0; i < total; i++) {
			doc = reader.document(i);
			size = new Long(doc.get(FilesContentIndexer.FIELD_SIZE));
			sizeanalyzer.addSize(size);
		}
		ArrayList<Integer> sizedistrib = sizeanalyzer.getDistribution();

		FileOutputStream fp;
		fp = new FileOutputStream(args[1]);

		String line;

		line = "Interval\tQuality\n";
		fp.write(line.getBytes("UTF-8"));

		total = sizedistrib.size();

		for (i = 0; i < total; i++) {
			line = new String("between " + i * intervel + " and " + (i + 1)
					* intervel + " characters");
			fp.write(line.getBytes("UTF-8"));
			line = "\t";
			fp.write(line.getBytes("UTF-8"));
			line = new Integer(sizedistrib.get(i)).toString();
			fp.write(line.getBytes("UTF-8"));
			line = "\n";
			fp.write(line.getBytes("UTF-8"));
			fp.flush();
		}

		fp.flush();
		fp.close();
	}

}
